
library(vistime)

names <- c("Cincinnatus","SR Kl�ber","Themopyles","Dutch Paris","Billet", "CND", "Abbe-Blanc","Jasmin","Eug�ne","Artist","Prosper","Gilbert","Geoffroy","Marco-Polo",
"Action P","Hi-Hi","Publican","Maurice","Gallia","Bourgogne","Hunter","Ajax","Androm�de-Ath�n�e","Akak","Mathilda","Tiburce","Alphonse","Orme",
"H�tre","Orph�e")


startdate <- as.Date(c('1940-08-01','1940-08-20','1940-10-01','1940-10-01','1940-11-01','1940-11-01','1941-06-01','1942-01-01','1942-07-01','1942-07-01','1942-09-24',
'1942-10-01','1942-11-01','1942-11-01','1942-11-01','1943-01-01','1943-01-27','1943-02-01','1943-02-01','1943-02-15','1943-02-18','1943-03-01','1943-06-13',
'1943-09-01','1943-10-01','1943-10-20','1943-12-01','1944-03-29','1944-05-09','1944-07-06'))

enddate <-as.Date(c('1942-12-31','1944-09-30','1944-09-30','1944-08-31', '1944-09-30','1943-11-05','1944-09-30','1944-09-30','1943-04-30', '1943-07-30','1943-06-30',
'1944-09-30','1944-09-30','1944-09-30','1944-09-30','1944-09-30','1943-09-20', '1944-09-30','1944-09-30','1944-09-30','1944-09-30','1944-09-30',
'1944-09-30','1944-09-30','1944-09-30','1944-09-30','1944-09-30','1944-09-30','1944-09-30','1944-09-30'))



data.frame <-data.frame(names, startdate, enddate, color = c(
'#ABABAB', '#ABABAB', '#ABABAB', '#ABABAB', '#ABABAB', '#ABABAB', '#ABABAB', '#737373', '#ABABAB',
'#ABABAB', '#737373', '#ABABAB', '#ABABAB', '#ABABAB', '#737373', '#ABABAB', '#ABABAB', '#737373',
'#ABABAB', '#ABABAB', '#ABABAB', '#737373', '#737373', '#ABABAB', '#ABABAB', '#ABABAB', '#ABABAB',
'#737373', '#737373', '#737373'))

vistime(data = data.frame, events = "names" ,start="startdate", end = "enddate", title="Sample of r�seau start and end dates", linewidth=15)

library(survminer)
library(survival)

splots <- list()

res.cox <- coxph(Surv(duration_months, terminationbeforeliberation) ~ londonsentfounder + ln_leadersarrested + ln_numagents +
      ln_numsubreseau + partofalargernetwork2 + activeintheoccupiedzone + v13 + britishcontrolled + americancontrolled + frenchcontrolled,
      data =  clandestineclients)




# Plot the baseline survival function
ggsurvplot(survfit(res.cox), color = "#2E9FDF", data=clandestineclients,
           ggtheme = theme_minimal(), ylim=c(0.5,1))

# Set your values
values <- with(clandestineclients,
               data.frame(londonsentfounder = c(0, 1),
                          ln_leadersarrested = rep(mean(ln_leadersarrested, na.rm = TRUE), 1),
                          ln_numagents = rep(mean(ln_numagents, na.rm = TRUE), 1),
                          ln_numsubreseau = rep(mean(ln_numsubreseau, na.rm = TRUE), 1),
                          partofalargernetwork2 = 0,
                          activeintheoccupiedzone = 0,
                          v13 = 0,
                          britishcontrolled = 0,
                          americancontrolled = 0,
                          frenchcontrolled = 0

                          )
               )


fit <- survfit(res.cox, newdata = values)

splots[[1]] = ggsurvplot(fit, conf.int = FALSE, data=fit, legend.title = "", legend.labs=c("Founded locally", "Operative founded"),
           ggtheme = theme_minimal(), ylim = c(0.6,1), xlim=c(0,40), xlab = "", title = "Group origins",
           palette = c("black", "gray"), font.title = c(11, "bold"), font.x = c(9, "bold"), font.y = c(9, "bold"),
           font.legend = c(9, "bold"), font.tickslab = c(8, "plain"), ylab="")



################
## Decapitation
################

res.cox <- coxph(Surv(duration_months, terminationbeforeliberation) ~ londonsentfounder + leaderslostdummy + ln_numagents +
      ln_numsubreseau + partofalargernetwork2 + activeintheoccupiedzone + v13 + britishcontrolled + americancontrolled +
      frenchcontrolled + militaryinforesearch + sabotage + economicintelgathering + politicalintelgathering, data =  clandestineclients)
summary(res.cox)

ggsurvplot(survfit(res.cox), color = "#2E9FDF", data=clandestineclients,
           ggtheme = theme_minimal(), ylim=c(0.5,1))

values <- with(clandestineclients,
               data.frame(londonsentfounder = 0,
                          leaderslostdummy = c(0,1),
                          ln_numagents = rep(mean(ln_numagents, na.rm = TRUE), 2),
                          ln_numsubreseau = rep(mean(ln_numsubreseau, na.rm = TRUE), 2),
                          partofalargernetwork2 = 0,
                          activeintheoccupiedzone = 0,
                          v13 = 0,
                          britishcontrolled = 0,
                          americancontrolled = 0,
                          frenchcontrolled = 0,
                          militaryinforesearch =0,
                          sabotage = 0,
                          economicintelgathering = 0,
                          politicalintelgathering = 0

                          )
               )

fit <- survfit(res.cox, newdata = values)

splots[[2]] = ggsurvplot(fit, conf.int = FALSE, data=fit, legend.title = "", legend.labs=c("Not decapitated", "Decapitated"),
           ggtheme = theme_minimal(), ylim = c(0.6,1), xlim=c(0,40), xlab = "", title = "Decapitation",
           palette = c("black", "gray"), font.title = c(11, "bold"), font.x = c(9, "bold"), font.y = c(9, "bold"),
           font.legend = c(9, "bold"), font.tickslab = c(8, "plain"), ylab="Cumulative survival probability")



#################
## Sabotage #####

res.cox <- coxph(Surv(duration_months, terminationbeforeliberation) ~ londonsentfounder + ln_leadersarrested + ln_numagents +
      ln_numsubreseau + partofalargernetwork2 + activeintheoccupiedzone + v13 + militaryinforesearch + sabotage + economicintelgathering + politicalintelgathering, data =  clandestineclients)
summary(res.cox)

ggsurvplot(survfit(res.cox), color = "#2E9FDF", data=clandestineclients,
           ggtheme = theme_minimal(), ylim=c(0.5,1))

values <- with(clandestineclients,
               data.frame(londonsentfounder = 0,
                          ln_leadersarrested = rep(mean(ln_leadersarrested, na.rm = TRUE), 2),
                          ln_numagents = rep(mean(ln_numagents, na.rm = TRUE), 2),
                          ln_numsubreseau = rep(mean(ln_numsubreseau, na.rm = TRUE), 2),
                          partofalargernetwork2 = 0,
                          activeintheoccupiedzone = 0,
                          v13 = 0,
                          militaryinforesearch =0,
                          sabotage = c(0,1),
                          economicintelgathering = 0,
                          politicalintelgathering = 0


                          )
               )

fit <- survfit(res.cox, newdata = values)

splots[[3]] = ggsurvplot(fit, conf.int = FALSE, data=fit, legend.title = "", legend.labs=c("Non-sabotage", "Sabotage"),
           ggtheme = theme_minimal(), ylim = c(0.6,1), xlim=c(0,40), xlab = "Time (months)", title = "Sabotage",
           palette = c("black", "gray"), font.title = c(11, "bold"), font.x = c(9, "bold"), font.y = c(9, "bold"),
           font.legend = c(9, "bold"), font.tickslab = c(8, "plain"), ylab="")





#################
## Group size #####

res.cox <- coxph(Surv(duration_months, terminationbeforeliberation) ~ londonsentfounder + ln_leadersarrested + groupsize +
      ln_numsubreseau + partofalargernetwork2 + activeintheoccupiedzone + v13 + militaryinforesearch + sabotage + economicintelgathering + politicalintelgathering, data =  clandestineclients)
summary(res.cox)

ggsurvplot(survfit(res.cox), color = "#2E9FDF", data=clandestineclients,
           ggtheme = theme_minimal(), ylim=c(0.5,1))

values <- with(clandestineclients,
               data.frame(londonsentfounder = 0,
                          ln_leadersarrested = rep(mean(ln_leadersarrested, na.rm = TRUE), 2),
                          groupsize = c(1:3,1),
                          ln_numsubreseau = rep(mean(ln_numsubreseau, na.rm = TRUE), 2),
                          partofalargernetwork2 = 0,
                          activeintheoccupiedzone = 0,
                          v13 = 0,
                          militaryinforesearch =0,
                          sabotage = 0,
                          economicintelgathering = 0,
                          politicalintelgathering = 0


                          )
               )

fit <- survfit(res.cox, newdata = values)


splots[[4]] = ggsurvplot(fit, conf.int = FALSE, data=fit, legend.title = "", legend.labs=c("", "Medium", "Large","Small"),
           ggtheme = theme_minimal(), ylim = c(0.6,1), xlim=c(0,40), xlab = "", title = "Group size",
           palette = c("white", "darkslategray", "black", "gray"), font.title = c(11, "bold"), font.x = c(9, "bold"), font.y = c(9, "bold"),
           font.legend = c(9, "bold"), font.tickslab = c(8, "plain"), ylab="")




################
## Active in occupied zone
################

res.cox <- coxph(Surv(duration_months, terminationbeforeliberation) ~ londonsentfounder + ln_leadersarrested + ln_numagents +
      ln_numsubreseau + partofalargernetwork2 + activeintheoccupiedzone + v13 + britishcontrolled + americancontrolled +
      frenchcontrolled + militaryinforesearch + sabotage + economicintelgathering + politicalintelgathering, data =  clandestineclients)
summary(res.cox)

ggsurvplot(survfit(res.cox), color = "#2E9FDF", data=clandestineclients,
           ggtheme = theme_minimal(), ylim=c(0.5,1))

values <- with(clandestineclients,
               data.frame(londonsentfounder = 0,
                          ln_leadersarrested = rep(mean(ln_leadersarrested, na.rm = TRUE), 2),
                          ln_numagents = rep(mean(ln_numagents, na.rm = TRUE), 2),
                          ln_numsubreseau = rep(mean(ln_numsubreseau, na.rm = TRUE), 2),
                          partofalargernetwork2 = 0,
                          activeintheoccupiedzone = c(0,1),
                          v13 = 0,
                          britishcontrolled = 0,
                          americancontrolled = 0,
                          frenchcontrolled = 0,
                          militaryinforesearch =0,
                          sabotage = 0,
                          economicintelgathering = 0,
                          politicalintelgathering = 0

                          )
               )

fit <- survfit(res.cox, newdata = values)

splots[[5]] = ggsurvplot(fit, conf.int = FALSE, data=fit, legend.title = "", legend.labs=c("Elsewhere", "Occupied zone"),
           ggtheme = theme_minimal(), ylim = c(0.6,1), xlim=c(0,40), xlab = "", title = "Area of activity",
           palette = c("black", "gray"), font.title = c(11, "bold"), font.x = c(9, "bold"), font.y = c(9, "bold"),
           font.legend = c(9, "bold"), font.tickslab = c(8, "plain"), ylab="")



################
## Part of larger network
################

res.cox <- coxph(Surv(duration_months, terminationbeforeliberation) ~ londonsentfounder + ln_leadersarrested + ln_numagents +
      ln_numsubreseau + partofalargernetwork2 + activeintheoccupiedzone + v13 + britishcontrolled + americancontrolled +
      frenchcontrolled + militaryinforesearch + sabotage + economicintelgathering + politicalintelgathering, data =  clandestineclients)
summary(res.cox)

ggsurvplot(survfit(res.cox), color = "#2E9FDF", data=clandestineclients,
           ggtheme = theme_minimal(), ylim=c(0.5,1))

values <- with(clandestineclients,
               data.frame(londonsentfounder = 0,
                          ln_leadersarrested = rep(mean(ln_leadersarrested, na.rm = TRUE), 2),
                          ln_numagents = rep(mean(ln_numagents, na.rm = TRUE), 2),
                          ln_numsubreseau = rep(mean(ln_numsubreseau, na.rm = TRUE), 2),
                          partofalargernetwork2 = c(0,1),
                          activeintheoccupiedzone = 0,
                          v13 = 0,
                          britishcontrolled = 0,
                          americancontrolled = 0,
                          frenchcontrolled = 0,
                          militaryinforesearch =0,
                          sabotage = 0,
                          economicintelgathering = 0,
                          politicalintelgathering = 0

                          )
               )

fit <- survfit(res.cox, newdata = values)

splots[[6]] = ggsurvplot(fit, conf.int = FALSE, data=fit, legend.title = "", legend.labs=c("No", "Yes"),
           ggtheme = theme_minimal(), ylim = c(0.6,1), xlim=c(0,40), xlab = "Time (months)", title = "Part of larger network",
           palette = c("black", "gray"), font.title = c(11, "bold"), font.x = c(9, "bold"), font.y = c(9, "bold"),
           font.legend = c(9, "bold"), font.tickslab = c(8, "plain"), ylab="")
           
arrange_ggsurvplots(splots, ncol = 2, nrow = 3)